/**
 * Created with JetBrains PhpStorm.
 * User: Guido
 * Date: 12/06/13
 * Time: 20:27
 * To change this template use File | Settings | File Templates.
 */
$(document).ready(function(){
    $( "#tabs" ).tabs();

    $('#tblEscenografia, #tblVestuario').dataTable();

    $('#tblDatos').dataTable({
        "aaSorting": [[ 1, "desc" ]]
    });

    $(".prevImg").click(function(){
        var sImgSrc = $(this).attr("alt");
        var oImg = $("#fullImg");
        oImg.attr("src","");
        oImg.attr("src",sImgSrc);
        $("#linkFullImg").attr("href",sImgSrc);
        $("#dialog-prevImg").dialog("open");
    });

    $( "#dialog-prevImg" ).dialog({
        autoOpen: false,
        resizable: false,
        height:600,
        width:800,
        modal: true,
        buttons: {
            "Cerrar": function() {
                $( this ).dialog( "close" );
            }
        }
    });

    $( ".loginBtn").button({
        icons: {
            primary: "ui-icon-unlocked"
        }
    }).click(function(){
            $("#formLogin").submit();
            return false;
        });

    $( ".logout").button({
        icons: {
            primary: "ui-icon-locked"
        }
    }).click(function(){
            $("#logoutForm").submit();
            return false;
        });

//############################################################
//############################################################
// Seccion Datos
//############################################################
//############################################################

    $( ".botonAddDato").button({
        icons: {
            primary: "ui-icon-circle-plus"
        }
    }).click(function(){
            var oPopUp = $( "#dialog-AddInfo" );
            oPopUp.dialog( "open" );

            return false;
        });

    $( ".botonDeleteDato").button({
        icons: {
            primary: "ui-icon-circle-close"
        },
        text:false
    }).click(function() {
            var iId = $(this).attr("id");

            var oPopUp = $( "#dialog-DeleteInfo" );
            var oTr = $("#tr_"+iId);
            oPopUp.dialog({ title: oTr.find(".col-nombre").html() });
            $("#idDeleteInfo").val( iId );
            oPopUp.dialog( "open" );

            return false;
        });

    $( ".botonEditDato").button({
        icons: {
            primary: "ui-icon-pencil"
        },
        text:false
    }).click(function() {

            var iId = $(this).attr("id");

            var oPopUp = $( "#dialog-editInfo" );
            var oTr = $("#tr_"+iId);
            oPopUp.dialog({ title: oTr.find(".col-nombre").html() });
            $(".valorInfo").val( trim(oTr.find(".col-valor").html()) );
            $("#idInfo").val( iId );
            oPopUp.dialog( "open" );

            return false;
        });



    $( "#dialog-AddInfo" ).dialog({
        autoOpen: false,
        resizable: false,
        height:300,
        width:250,
        modal: true,
        buttons: {
            "Guardar": function() {
                $("#formAddInfo").submit();
            },
            "Cancelar": function() {
                $( this ).dialog( "close" );
            }
        }
    });
    $( "#dialog-DeleteInfo" ).dialog({
        autoOpen: false,
        resizable: false,
        height:160,
        width:250,
        modal: true,
        buttons: {
            "Aceptar": function() {
                $("#formDeleteInfo").submit();
            },
            "Cancelar": function() {
                $( this ).dialog( "close" );
            }
        }
    });
    $( "#dialog-editInfo" ).dialog({
        autoOpen: false,
        resizable: false,
        height:155,
        width:250,
        modal: true,
        buttons: {
            "Guardar": function() {
                $("#formInfo").submit();
            },
            "Cancelar": function() {
                $( this ).dialog( "close" );
            }
        }
    });


//############################################################
//############################################################
// Seccion Escenografia
//############################################################
//############################################################
    $( ".botonEditEscen").button({
        icons: {
            primary: "ui-icon-pencil"
        },
        text:false
    }).click(function(){
            var imgId = $(this).attr("id");
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: { request:"imgInfo" ,imgId: imgId }
            }).done(function( data ) {
                    var oData = eval('(' + data + ')');

                    if (oData[0].idcategoria != 0){
                        $("#previewUploadImgEsc").attr("src","../"+oData[0].path + "alquiler/" + oData[0].filename);
                        $("#imgName").val(oData[0].filename);

                        $("#cmbCategoriaEsc").val(oData[0].idcategoria);
                        $("#txtTituloAlqEsc").val(oData[0].titulo);
                        $("#idImgAlqEsc").val(oData[0].id);

                        $("#dialog-addEscAlquiler").dialog("open")

                    }else{
                        $("#previewUploadImg2Esc").attr("src","../"+oData[0].path + "realizacion/" + oData[0].filename);
                        $("#imgNameRealizEsc").val(oData[0].filename);
                        $("#txtTituloRealizEsc").val(oData[0].titulo);
                        $("#idImgRealizEsc").val(oData[0].id);

                        $("#dialog-addEscRealiz").dialog("open")

                    }
            });
        });

    $( ".botonDelEscen").button({
        icons: {
            primary: "ui-icon-circle-close"
        },
        text:false
    }).click(function(){
            if(confirm("Esta seguro que desea borrar este registro?")){
                $("#deleteImgId").val($(this).attr("id"));
                $("#formDeleteImg").submit();
            }
        });

    $( ".botonAddRealizEsc").button({
        icons: {
            primary: "ui-icon-circle-plus"
        }
    }).click(function(){
            $("#dialog-addEscRealiz").dialog("open");
        });

    $( ".botonAddAlqEsc").button({
        icons: {
            primary: "ui-icon-circle-plus"
        }
    }).click(function(){
            $("#dialog-addEscAlquiler").dialog("open");
        });

    $( "#dialog-addEscAlquiler" ).dialog({
        autoOpen: false,
        resizable: false,
        width:640,
        height:480,
        modal: true,
        buttons: {
            "Guardar": function() {
               $("#formAddEscAlq").submit();
            },
            "Cancelar": function() {
                $( this ).dialog( "close" );
            }
        }
    });
    $( "#dialog-addEscRealiz" ).dialog({
        autoOpen: false,
        resizable: false,
        width:640,
        height:480,
        modal: true,
        buttons: {
            "Guardar": function() {
               $("#formAddEscRealiz").submit();
            },
            "Cancelar": function() {
                $( this ).dialog( "close" );
            }
        }
    });

    $("#file_upload_Esc_Alq").uploadify({
        'formData'     : {
            'path' : UPLOAD_FOLDER_ESC_ALQ
        },
        height        : 30,
        swf           : '../uploadify.swf',
        uploader      : '../uploadify.php',
        width         : 120,
        'fileSizeLimit' : '10MB',
        'onUploadSuccess' : function(file, data, response) {
                var fileFullPath = DOCUMENT_ROOT+"/"+UPLOAD_FOLDER_ESC_ALQ+data;
                var fileFullPathThum = DOCUMENT_ROOT+"/"+UPLOAD_FOLDER_ESC_ALQ+"/thum/"+data;
                var oImg = $("#previewUploadImgEsc");
                oImg.attr('src',fileFullPathThum);
                oImg.attr('alt',fileFullPath);
                $("#imgName").val(data);
        }
    });

    $("#file_upload_Esc_Realiz").uploadify({
        'formData'     : {
            'path' : UPLOAD_FOLDER_ESC_REALIZ
        },
        height        : 30,
        swf           : '../uploadify.swf',
        uploader      : '../uploadify.php',
        width         : 120,
        'fileSizeLimit' : '10MB',
        'onUploadSuccess' : function(file, data, response) {
                var fileFullPath = DOCUMENT_ROOT+"/"+UPLOAD_FOLDER_ESC_REALIZ+data;
                var fileFullPathThum = DOCUMENT_ROOT+"/"+UPLOAD_FOLDER_ESC_REALIZ+"/thum/"+data;
                var oImg = $("#previewUploadImg2Esc");
                oImg.attr('src',fileFullPathThum);
                oImg.attr('alt',fileFullPath);
                $("#imgNameRealizEsc").val(data);
        }
    });



//############################################################
//############################################################
// Seccion Vestuario
//############################################################
//############################################################
    $( ".botonEditVes").button({
        icons: {
            primary: "ui-icon-pencil"
        },
        text:false
    }).click(function(){
            var imgId = $(this).attr("id");
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: { request:"imgInfo" ,imgId: imgId }
            }).done(function( data ) {
                    var oData = eval('(' + data + ')');

                    if (oData[0].idcategoria != 0){
                        $("#previewUploadImgVes").attr("src","../"+oData[0].path + "alquiler/" + oData[0].filename);
                        $("#imgName").val(oData[0].filename);

                        $("#cmbCategoriaVes").val(oData[0].idcategoria);
                        $("#txtTituloAlqVes").val(oData[0].titulo);
                        $("#idImgAlqVes").val(oData[0].id);

                        $("#dialog-addVesAlquiler").dialog("open")
                    }else{
                        $("#previewUploadImg2Ves").attr("src","../"+oData[0].path + "realizacion/" + oData[0].filename);
                        $("#imgNameRealizVes").val(oData[0].filename);
                        $("#txtTituloRealizVes").val(oData[0].titulo);
                        $("#idImgRealizVes").val(oData[0].id);

                        $("#dialog-addVesRealiz").dialog("open")
                    }
                });
        });

    $( ".botonDelVes").button({
        icons: {
            primary: "ui-icon-circle-close"
        },
        text:false
    }).click(function(){
            if(confirm("Esta seguro que desea borrar este registro?")){
                $("#deleteImgId").val($(this).attr("id"));
                $("#formDeleteImg").submit();
            }
        });

    $( ".botonAddRealizVes").button({
        icons: {
            primary: "ui-icon-circle-plus"
        }
    }).click(function(){
            $("#dialog-addVesRealiz").dialog("open");
        });

    $( ".botonAddAlqVes").button({
        icons: {
            primary: "ui-icon-circle-plus"
        }
    }).click(function(){
            $("#dialog-addVesAlquiler").dialog("open");
        });

    $( "#dialog-addVesAlquiler" ).dialog({
        autoOpen: false,
        resizable: false,
        width:640,
        height:480,
        modal: true,
        buttons: {
            "Guardar": function() {
                $("#formAddVesAlq").submit();
            },
            "Cancelar": function() {
                $( this ).dialog( "close" );
            }
        }
    });
    $( "#dialog-addVesRealiz" ).dialog({
        autoOpen: false,
        resizable: false,
        width:640,
        height:480,
        modal: true,
        buttons: {
            "Guardar": function() {
                $("#formAddVesRealiz").submit();
            },
            "Cancelar": function() {
                $( this ).dialog( "close" );
            }
        }
    });

    $("#file_upload_Ves_Alq").uploadify({
        'formData'     : {
            'path' : UPLOAD_FOLDER_VES_ALQ
        },
        height        : 30,
        swf           : '../uploadify.swf',
        uploader      : '../uploadify.php',
        width         : 120,
        'fileSizeLimit' : '10MB',
        'onUploadSuccess' : function(file, data, response) {
            var fileFullPath = DOCUMENT_ROOT+"/"+UPLOAD_FOLDER_VES_ALQ+data;
            var fileFullPathThum = DOCUMENT_ROOT+"/"+UPLOAD_FOLDER_VES_ALQ+"/thum/"+data;
            var oImg = $("#previewUploadImgVes");
            oImg.attr('src',fileFullPathThum);
            oImg.attr('alt',fileFullPath);
            $("#imgNameVes").val(data);
        }
    });

    $("#file_upload_Ves_Realiz").uploadify({
        'formData'     : {
            'path' : UPLOAD_FOLDER_VES_REALIZ
        },
        height        : 30,
        swf           : '../uploadify.swf',
        uploader      : '../uploadify.php',
        width         : 120,
        'fileSizeLimit' : '10MB',
        'onUploadSuccess' : function(file, data, response) {
            var fileFullPath = DOCUMENT_ROOT+"/"+UPLOAD_FOLDER_VES_REALIZ+data;
            var fileFullPathThum = DOCUMENT_ROOT+"/"+UPLOAD_FOLDER_VES_REALIZ+"/thum/"+data;
            var oImg = $("#previewUploadImg2Ves");
            oImg.attr('src',fileFullPathThum);
            oImg.attr('alt',fileFullPath);
            $("#imgNameRealizVes").val(data);
        }
    });

});
//############################################################
//############################################################
// Funciones Generales
//############################################################
//############################################################



function trim (myString)
{
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}